首页>代码>java项目常用开发工具类>/新建文件夹 (4)/ChineseSpelling.java
package com.ylpw.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import org.apache.log4j.Logger;

/**
 * @Author ZJJ
 * @Date Nov 19, 2011
 * @Time 11:04:45 AM 类说明
 */
public class ChineseSpelling {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ChineseSpelling.class);
	 /**   
     * 汉字转换位汉语拼音首字母,英文字符不变   
     * @param chinese 汉字   
     * @return 拼音   
     */      
    public static String converterToFirstSpell(String chinese){              
         String pinyinName = "";       
        char[] nameChar = chinese.toCharArray();       
         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();       
         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);       
         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);       
        for (int i = 0; i < nameChar.length; i++) {       
            if (nameChar[i] > 128) {       
                try {       
                     pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);       
                 } catch (BadHanyuPinyinOutputFormatCombination e) {
                	 logger.error(e.getMessage(), e);
                 }       
             }else{       
                 pinyinName += nameChar[i];       
             }       
         }       
        return pinyinName;       
     }       
        
    /**   
     * 汉字转换位汉语拼音,英文字符不变   
     * @param chinese 汉字   
     * @return 拼音   
     */      
    public static String converterToSpell(String chinese){               
         String pinyinName = "";       
        char[] nameChar = chinese.toCharArray();       
         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();       
         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);       
         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);       
        for (int i = 0; i < nameChar.length; i++) {       
            if (nameChar[i] > 128) {       
                try {       
                     pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];       
                 } catch (BadHanyuPinyinOutputFormatCombination e) {
                	 logger.error(e.getMessage(), e);
                 }       
             }else{       
                 pinyinName += nameChar[i];       
             }       
         }       
        return pinyinName;       
     }   
    
    
    public static void main(String[] args) {      
    }     

}
最近下载更多
一个好人520  LV10 2021年9月29日
叽哩咕噜  LV2 2020年12月24日
wangdongtai  LV31 2020年11月6日
xcj456  LV8 2020年9月12日
moomin709  LV24 2020年7月6日
Gyq灬ming  LV11 2020年6月4日
xuyongff  LV24 2019年11月19日
托马斯奎  LV6 2019年8月5日
qq1453363097  LV13 2019年3月26日
高利杰  LV5 2018年12月19日
最近浏览更多
清清河边草 2023年10月11日
暂无贡献等级
漫步的海星  LV4 2023年3月14日
菜鸟666  LV2 2022年8月10日
17782780328  LV1 2022年7月21日
ewan007  LV29 2022年7月8日
a3870764722a  LV22 2022年6月7日
喃喵xxxx  LV6 2022年4月20日
crosa_Don  LV18 2022年4月1日
nbzhou2013  LV14 2022年3月18日
happyYang 2022年1月7日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友